package com.atguigu.tingshu.user.mq;


import com.alibaba.fastjson.JSON;
import com.atguigu.tingshu.common.constant.KafkaConstant;


import com.atguigu.tingshu.user.service.UserInfoService;
import com.atguigu.tingshu.vo.user.UserPaidRecordVo;
import lombok.extern.slf4j.Slf4j;

import org.apache.kafka.clients.consumer.ConsumerRecord;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.sql.Connection;

@Component
@Slf4j
public class UserReceiver {


    @Autowired
    private  UserInfoService userInfoService;

    @KafkaListener(topics = KafkaConstant.QUEUE_USER_PAY_RECORD )
    public void  updateUserPaidRecord(ConsumerRecord<String,String> record){


        String value = record.value();
        if(!StringUtils.isEmpty(value)){  //获取到发送到kafka的交易信息
            UserPaidRecordVo userPaidRecordVo = JSON.parseObject(value, UserPaidRecordVo.class);
            userInfoService.updateUserPaidRecord(userPaidRecordVo);
            log.info("发送消息成功,发送到了kafka");
        }
    }


}
